/**
 * 应用页面跳转路由器
 * @type type
 */
App.Router = (function(lng) {

    var _view = App.View;
    var _presenter = App.Presenter;
    var CLASS_CURRENT = 'current';

    /**
     * 
     * @param {String} section_id
     * @returns {undefined}
     */
    var gotoSection = function(section_id) {
        var section_code = '#' + section_id;
        if (!lng.Router.backTo(section_code, _onTrigger)) {
            _onTrigger(null, section_code);
            _onTrigger(null);
            lng.Router.section(section_id);
        }
    };

    /**
     * 
     * @param {type} section_id
     * @param {type} article_id
     * @param {type} nav id
     * @param {type} loadData
     * @returns {undefined}
     */
    var gotoArticle = function(section_id, article_id, nav_id, loadData) {
        gotoSection(section_id, true);
        lng.Router.article(section_id, article_id);
        _view.showPage(article_id);
        nav_id && showNavItem(nav_id);
        window.setTimeout(function() {
            _presenter.loadPanel(article_id);
            loadData && loadData();
        }, 200);
    };

    /**
     * 
     * @returns {undefined}
     */
    var goBack = function() {
        lng.Router.back(_onTrigger);
        _presenter.loadPanel(lng.Element.Cache.article[0].id);
    };

    /**
     * on section trigger, push or pull section
     * 
     * @param {type} section
     * @param {type} target_id
     * @returns {undefined}
     */
    var _onTrigger = function(section, target_id) {
        if (!target_id) {
            _view.pushCachePage(section);
        } else {
            _view.pullCachePage(target_id);
        }
    };

    /**
     * 
     * @param {type} nav
     * @param {type} fnav
     * @returns {undefined}
     */
    var showNavItem = function(navs, hnav) {
        var fnav = navs;
        if ((navs instanceof Array) && navs.length > 1) {
            fnav = navs[0];
            hnav = navs[1];
        }
        if (fnav) {
            // remove current nav style
            var navs = lng.Element.Cache.section.find('a[href][data-router="nav"]');
            navs && navs.removeClass(CLASS_CURRENT);
            // set new nav style
            lng.dom('#' + fnav).addClass(CLASS_CURRENT);
        }
        if (hnav) {
            // set footer nav style
            lng.dom('#' + hnav).addClass(CLASS_CURRENT);
        }
    };

    return {
        gotoSection: gotoSection,
        gotoArticle: gotoArticle,
        goBack: goBack,
        showNavItem: showNavItem
    };
})(Lungo);